package org.jeecg.modules.basic.attendance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.basic.attendance.entity.LwgyInoutAttendance;
import org.jeecg.modules.basic.attendance.entity.LwgyInoutAttendanceFormStu;
import org.jeecg.modules.basic.attendance.mapper.LwgyInoutAttendanceMapper;
import org.jeecg.modules.basic.attendance.service.ILwgyInoutAttendanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 考勤记录
 * @Author: jeecg-boot
 * @Date:   2024-06-03
 * @Version: V1.0
 */
@Service
public class LwgyInoutAttendanceServiceImpl extends ServiceImpl<LwgyInoutAttendanceMapper, LwgyInoutAttendance> implements ILwgyInoutAttendanceService {
    @Autowired
    private LwgyInoutAttendanceMapper lwgyInoutAttendanceMapper;

    public IPage<LwgyInoutAttendanceFormStu> queryPageListKqInfo(Page<LwgyInoutAttendanceFormStu> page, QueryWrapper<LwgyInoutAttendanceFormStu> queryWrapper) {
        queryWrapper.isNotNull("t1.id");
        queryWrapper.orderByAsc("t1.bed_code");
        List<LwgyInoutAttendanceFormStu> list =  lwgyInoutAttendanceMapper.queryPageListKqInfo(page,queryWrapper);


    /*    List<String> numberList = list.stream().map(LwgyInoutAttendanceFormStu::getStudentNumber).collect(Collectors.toList());

        QueryWrapper<LwgyInoutAttendance> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.in("student_number", numberList);
// 按时间倒序排序
        queryWrapper2.orderByDesc("sign_time");
// 使用分组(GROUP BY)来获取每个学号的第一条数据
        queryWrapper2.select("MIN(id) as kqid, student_number, MIN(sign_time) as signTime");
        queryWrapper2.groupBy("student_number");
// 执行查询
        List<LwgyInoutAttendance> LwgyInoutAttendancelist = lwgyInoutAttendanceMapper.selectList(queryWrapper2);

        for (LwgyInoutAttendanceFormStu item:list) {
            for (LwgyInoutAttendance item2 : LwgyInoutAttendancelist){
                if(item.getStudentNumber() .equals( item2.getStudentNumber())){
                    item.setEndkqId(item2.getId());
                    item.setEndkqTime(item2.getSignTime());
                    item.setEndkqLocation(item2.getPosition());
                    item.setEndkqResult(item2.getSignResult());

                }
            }
           // if(item.get)
        }*/


// students现在包含了按学号分组的，按时间排序后的每组的第一条数据
        return page.setRecords(list);
    }

    public void updateSignHandleById (LwgyInoutAttendance lwgyInoutAttendance){
        String id=lwgyInoutAttendance.getId();
        String signHandle = lwgyInoutAttendance.getSignHandle();
        lwgyInoutAttendanceMapper.updateSignHandleById( id, signHandle);
    }

    public List<LwgyInoutAttendance> queryKqByStuNumber(String studentNumber){
        return lwgyInoutAttendanceMapper.queryKqByStuNumber(studentNumber);
    }
}
